$(function() {

	var OPINION_STATE = '已评';
	var OPINION_STATE_MILLIONS = 500;
	var NO_LOGIN = 401;
	var NO_LOGIN_MSG = '请先登录！';

	var articleId = common.getParam('article_id');

	/**
	 * 显示博文详情
	 */
	var $artileDetail = $('#artileDetail');
	common.ajax($artileDetail, 'getArticleInfo?article_id=' + articleId, function(data) {
		common.render($artileDetail, $('#artileDetailTpl'), {
			article: data.result
		});
	});

	/**
	 * 显示博文的评论信息
	 */
	var $disscuss = $('#disscuss');

	function listDiscuss() {
		common.ajax($disscuss, 'listDiscuss?article_id=' + articleId, function(data) {
			common.render($disscuss, $('#discussTpl'), {
				disscusses: data.result
			});
		});
	}
	listDiscuss();

	/**
	 * 对指定的评论点赞或否定（点赞或者否定，type为true表示点赞，type为false表示否定）
	 */
	var $agreeShow;
	var preAgreeNumber = 0;
	var agreeSetTimeoutId;
	$(document).one('click', '.fa-thumbs-o-up', agree);

	function agree() {
		var token = common.checkToken();
		if (!token) {
			return;
		}

		var $that = $(this);
		$agreeShow = $that.children('span');
		var param = {
			type: true,
			discuss_id: $(this).data('disscussid'),
			token: token,
			time: new Date().getTime()
		};
		$.get(common.path + 'agreeOrArgue', param, function(data) {
			if (data.code == NO_LOGIN) {
				layer.msg(NO_LOGIN_MSG);
			} else {
				if (data.result) {
					preAgreeNumber = Number($agreeShow.html()) + 1;
					$agreeShow.html(OPINION_STATE);
					setTimeout(function() {
						$agreeShow.html(preAgreeNumber);
					}, OPINION_STATE_MILLIONS);
					$that.css('cursor', 'default');
					$(document).off('click', '.fa-thumbs-o-up');
				}
			}
		})
	}
	$(document).on('mouseenter', '.fa-thumbs-o-up', function() {
		var $that = $(this);
		$agreeShow = $that.children('span');

		var text = $agreeShow.html();
		if (text != OPINION_STATE) {
			preAgreeNumber = Number(text);
		}

		clearTimeout(agreeSetTimeoutId);
		agreeSetTimeoutId = setTimeout(function() {
			var param = {
				discuss_id: $that.data('disscussid'),
				token: sessionStorage.getItem('token'),
				time: new Date().getTime()
			};
			$.get(common.path + 'isOpinion', param, function(data) {
				if (data.reason == OPINION_STATE) {
					$agreeShow.html(OPINION_STATE);
					setTimeout(function() {
						$agreeShow.html(preAgreeNumber);
					}, OPINION_STATE_MILLIONS);
					$that.css('cursor', 'default');
					$(document).off('click', '.fa-thumbs-o-up');
				}
			})
		}, 300);
	});
	$(document).on('mouseleave', '.fa-thumbs-o-up', function() {
		$(document).off('click', '.fa-thumbs-o-up').one('click', '.fa-thumbs-o-up', agree);
	});

	var $argueShow;
	var preArgueNumber = 0;
	var argueSetTimeoutId;
	$(document).on('click', '.fa-thumbs-o-down', argue);

	function argue() {
		var token = common.checkToken();
		if (!token) {
			return;
		}

		var $that = $(this);
		$argueShow = $that.children('span');

		var param = {
			type: false,
			discuss_id: $(this).data('disscussid'),
			token: token,
			time: new Date().getTime()
		};
		$.get(common.path + 'agreeOrArgue', param, function(data) {
			if (data.code == NO_LOGIN) {
				layer.msg(NO_LOGIN_MSG);
			} else {
				if (data.result) {
					preArgueNumber = Number($argueShow.html()) + 1;
					$argueShow.html(OPINION_STATE);
					setTimeout(function() {
						$argueShow.html(preArgueNumber);
					}, OPINION_STATE_MILLIONS);
					$that.css('cursor', 'default');
					$(document).off('click', '.fa-thumbs-o-down');
				}
			}
		})
	};
	$(document).on('mouseenter', '.fa-thumbs-o-down', function() {
		var $that = $(this);
		$argueShow = $that.children('span');

		var text = $argueShow.html();
		if (text != OPINION_STATE) {
			preArgueNumber = Number(text);
		}

		clearTimeout(argueSetTimeoutId);
		argueSetTimeoutId = setTimeout(function() {
			var param = {
				discuss_id: $that.data('disscussid'),
				token: sessionStorage.getItem('token'),
				time: new Date().getTime()
			};
			$.get(common.path + 'isOpinion', param, function(data) {
				if (data.reason == OPINION_STATE) {
					$argueShow.html(OPINION_STATE);
					setTimeout(function() {
						$argueShow.html(preArgueNumber);
					}, OPINION_STATE_MILLIONS);
					$that.css('cursor', 'default');
					$(document).off('click', '.fa-thumbs-o-down');
				}
			})
		}, 300);
	});
	$(document).on('mouseleave', '.fa-thumbs-o-down', function() {
		$(document).off('click', '.fa-thumbs-o-down').one('click', '.fa-thumbs-o-down', argue);
	});

	/**
	 * 发表评论
	 */
	var $disucssContent = $('#disucssContent');
	$('#addDiscussBtn').click(function() {
		var $that = $(this);

		var token = common.checkToken();
		if (!token) {
			return;
		}

		// 防止重复提交
		$that.prop('disabled', true);
		$that.html('正在发表评论...');

		var param = {
			'token': token,
			'discuss.article_id': articleId,
			'discuss.content': $disucssContent.val().trim()
		};
		$.post(common.path + 'addDiscuss', param, function(data) {
			if (data.code == NO_LOGIN) {
				layer.msg(NO_LOGIN_MSG);
			} else if (data.code == 412) {
				layer.msg(data.reason);
			} else {
				$disscuss.children('li:gt(0)').remove();
				listDiscuss();
				$disucssContent.val('');
				layer.msg('评论成功！');
			}
			$that.prop('disabled', false);
			$that.html('发表');
		});
	});
});